var add,edit,setVal,reset;
require(['select2', 'date','layout','validator'], function() {
	var $formData = $('#formData');
	$formData.initForm();
	
	add = function(fn) {
		addOrUpdate("/baseCarInfo/addCarInfo.htm",fn);
	};
	
	edit = function(fn) {
		addOrUpdate("/baseCarInfo/uptCarInfo.htm",fn);
	};
	
	setVal = function (carId){
		$.ajaxExt({
			url :"/baseCarInfo/getCarInfoByCarId.htm",
			data : {'carId' : carId}
		}).done(function(data){
			$formData.setValues(data);
//			console.info($formData.getValues());
		});
	};
	
	reset = function (){
		$formData.reset($formData);
	};
	
	function addOrUpdate(url,callBack){
		var result = $.validator($formData);
		$.ajaxExt({
			url: '/baseOrgan/getBaseOrganList.htm',
			loadMask:true
			//async: false;
		}).done(function(data){
			var params = ridSpane();
				$.each(data, function(i, res){
					if($("input[name='organId']").val() == res.organId){
						$("input[name='corpId']").val(res.corpId);
						params.corpId = res.corpId;
						if(result){
							$.ajaxExt({
								url : url,
								data : params
							}).done(function(){
								callBack();
							});
						}
						return;
					}
			});
		});
	}
	
	function ridSpane(){
		var allValues = $formData.getValues();
		allValues.licPltNo = allValues.licPltNo.replace(/\s+/g, "");
		allValues.carModel = allValues.carModel.replace(/\s+/g, "");
		allValues.carInCd = allValues.carInCd.replace(/\s+/g, "");
		return allValues;
	}
});